package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.VO.ElectricVO;
import com.company.project.model.VO.UserVO;
import com.company.project.model.VO.ZhusuVO;
import com.company.project.model.VtElectricity;
import com.company.project.model.VtManager;
import com.company.project.model.VtZhusu;
import com.company.project.service.VtElectricityService;
import com.company.project.service.VtManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.type.NStringTypeHandler;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created by CodeGenerator on 2023/11/07.
*/
@RestController
@RequestMapping("/vt/electricity")
public class VtElectricityController {
    @Resource
    private VtElectricityService vtElectricityService;
    @Resource
    private VtManagerService vtManagerService;
    @PostMapping("/add")
    public Result add(@RequestBody VtElectricity vtElectricity,@RequestHeader("token") String token) {
        UserVO userVO = vtManagerService.findUserByToken(token);
        Integer groupId = null;
        if (userVO.getRoleId() == 4 && userVO.getGroupId() == 17) {
            groupId =13;
        }else if (userVO.getRoleId() == 4 && userVO.getGroupId() == 18) {
            groupId =14;
        }else if (userVO.getRoleId() == 4 && userVO.getGroupId() == 19) {
            groupId =15;
        }
        vtElectricity.setGroupid(groupId);
        vtElectricityService.save(vtElectricity);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        vtElectricityService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody VtElectricity vtElectricity) {
        vtElectricityService.update(vtElectricity);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        VtElectricity vtElectricity = vtElectricityService.findById(id);
        return ResultGenerator.genSuccessResult(vtElectricity);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<VtElectricity> list = vtElectricityService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/electricList")
    public Result electricList(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,@RequestHeader("token") String token) {
        UserVO userVO = vtManagerService.findUserByToken(token);
        Integer groupId = null;
        if (userVO.getRoleId() == 4 && userVO.getGroupId() == 17) {
            groupId =13;
        }else if (userVO.getRoleId() == 4 && userVO.getGroupId() == 18) {
            groupId =14;
        }else if (userVO.getRoleId() == 4 && userVO.getGroupId() == 19) {
            groupId =15;
        }

        PageHelper.startPage(page, size);
        List<VtElectricity> list =vtElectricityService.findElectricListByGroupId(groupId);
        List<ElectricVO> electricVOS = new ArrayList<>();
        for (VtElectricity temp:list) {
            ElectricVO electricVO = new ElectricVO();
            electricVO.setId(temp.getId());
            electricVO.setGroupid(temp.getGroupid());
            electricVO.setsElectricity(temp.getsElectricity());
            electricVO.setxElectricity(temp.getxElectricity());
            electricVO.setTime(temp.getTime());
            long timestamp = temp.getTime() * 1000L;
            Date date = new Date(timestamp); // 将时间戳转换为Date对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 指定日期格式
            String formattedDate = sdf.format(date);
            electricVO.setTimeString(formattedDate);
            electricVOS.add(electricVO);
        }
        PageInfo pageInfo = new PageInfo(electricVOS);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
